package com.hghivln.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hghivln.blog.entity.Type;
import com.hghivln.blog.mapper.BlogMapper;
import com.hghivln.blog.mapper.TypeMapper;
import com.hghivln.blog.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 帖子类型表 服务实现类
 * </p>
 *
 * @author hghivln
 * @since 2021-09-03
 */
@Service
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type> implements TypeService {

    @Autowired
    private BlogMapper blogMapper;

    @Override
    public List<Type> getAllList() {
        QueryWrapper<Type> qw = new QueryWrapper<>();
        qw.eq("deleted", 0);
        List<Type> typeList = getBaseMapper().selectList(qw);
        // 分组查询每个分类的博客数
        List<Type> countList = blogMapper.getTypeCount();
        typeList.forEach(e -> {
            Type type = countList.stream().filter(t -> t.getTypeId().equals(e.getTypeId())).findFirst().orElse(new Type());
            if (type.getTypeBlogCount() == null) {
                e.setTypeBlogCount(0);
            } else {
                e.setTypeBlogCount(type.getTypeBlogCount());
            }
        });
        return typeList;
    }

    @Override
    public void deleteById(Long id) {
        getBaseMapper().delById(id);
    }


}
